<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>scope manual page - </TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">[incr Tcl] Package Commands, version 4.2.1</a> <small>&gt;</small> scope</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
itcl::scope &mdash; capture the namespace context for a variable
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>itcl::scope </B><I>name</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
Creates a scoped value for the specified <I>name</I>, which must
be a variable name.  If the <I>name</I> is an instance variable,
then the scope command returns a name which will resolve in any
context as an instance variable belonging to <I>object</I>.
The precise format of this name is an internal detail to Itcl.
Use of such a scoped value makes it possible to use
instance variables in conjunction with widgets.  For example, if you
have an object with a private variable <TT>x</TT>, and you can use
<TT>x</TT> in conjunction with the <TT>-textvariable</TT> option of an
entry widget.  Before itcl3.0, only common variables could be used
in this manner.
<P>
If the <I>name</I> is not an instance variable, then it must be
a common variable or a global variable.  In that case, the scope
command returns the fully qualified name of the variable, e.g.,
<TT>::foo::bar::x</TT>.
<P>
If the <I>name</I> is not recognized as a variable, the scope
command returns an error.
<P>
Ordinary variable names refer to variables in the global namespace.
A scoped value captures a variable name together with its namespace
context in a way that allows it to be referenced properly later.
It is needed, for example, to wrap up variable names when a Tk
widget is used within a namespace:
<PRE>namespace foo {
    private variable mode 1

    radiobutton .rb1 -text &quot;Mode #1&quot; 
        -variable [scope mode] -value 1
    pack .rb1

    radiobutton .rb2 -text &quot;Mode #2&quot; 
        -variable [scope mode] -value 2
    pack .rb2
}</PRE>
Radiobuttons <TT>.rb1</TT> and <TT>.rb2</TT> interact via the variable
&quot;mode&quot; contained in the namespace &quot;foo&quot;.  The <B>scope</B> command
guarantees this by returning the fully qualified variable name
<TT>::foo::mode</TT>.
<P>
You should never attempt to craft your own scoped variable names,
even if you believe you've flawlessly reverse-engineered the encoding.
Instead, you should always use the scope command to generate the
variable name dynamically.  Then, you can pass that name to a widget
or to any other bit of code in your program.

<H3><A NAME="M5">KEYWORDS</A></H3>
<A href="../Keywords/C.htm#code">code</A>, <A href="../Keywords/N.htm#namespace">namespace</A>, <A href="../Keywords/V.htm#variable">variable</A>
<div class="copy">Copyright &copy; 1993-1998 Lucent Technologies, Inc.
</div>
</BODY></HTML>
